

local assert     = assert
local require    = require
local bit        = bit
local coroutine  = coroutine
local debug      = debug
local io         = io
local pairs      = pairs
local ipairs     = ipairs
local math       = math
local os         = os
local print      = print
local pcall      = pcall
local xpcall     = xpcall
local rawget     = rawget
local rawset     = rawset
local select     = select
local string     = string
local table      = table
local tonumber   = tonumber
local tostring	 = tostring
local error      = error
local type       = type
local unpack     = unpack
local setmetatable = setmetatable
local getmetatable = getmetatable
local ngx = ngx

local string_char  = string.char
local string_sub   = string.sub
local string_byte  = string.byte
local string_gsub  = string.gsub
local string_find  = string.find
local string_upper = string.upper

local math_fmod   = math.fmod
local math_floor  = math.floor
local math_pow    = math.pow
local math_abs    = math.abs

local table_tostring = table.tostring
local ngx_shared     = ngx.shared


----数据变量----
local uu           = require( "modules.commhm.utils" )
local log          = require( "modules.commhm.log" )
local ns_time      = require( "modules.commhm.time" )
local ns_network   = require( "modules.commhm.network" )

local ns_redisMgrDc = require( "modules.commhm.dc.redisMgrDc" );
local ns_config     = require( "modules.data_cache.data_cache_config" )

local ns_data_cache_ip_allow  = require( "modules.data_cache.data_cache_ip_allow" )


----------------------------------------------------
log.debug( "init gg----" );
--ngx.cache = ngx.cache or {};   --全局变量区

--上下文变量
--ngx.ctx = {
	--m_params        = {},
	--m_profile_data  = {},
	--m_ma_task_data  = {},
	--m_user_data_is_dirty = false,
--}


local FOO = {
	_VERSION = '0.12.1',
	__INT32__  = 2147483648;	
	__UINT32__ = 4294967296;

	redisMgrDcList = false;
};


FOO.KEYS = {
};


FOO.init = function()
	log.debug( "gg init" );

	if  not FOO.redisMgrDcList then
		FOO.redisMgrDcList = {}
		if  ns_config.redis then
			for k, v in pairs( ns_config.redis ) do
				--- openresty_dc_x.conf 中有配置这个 shared_dict
				if  ngx_shared[ k ..'0' ] or ngx_shared[ k ] then
					if  type(v) == 'table' and v.redis_server and v.redis_db_select then
						log.debug( "create new redis_mgr: " .. k );
						FOO.redisMgrDcList[k] = ns_redisMgrDc:new( k, v.redis_server,  v.redis_db_select );

						if  v.ip_allow then
							ns_data_cache_ip_allow.add_ip_allow( k, v.ip_allow )   ---IP白名单
						else
							log.error( "WARNING:not_set_dc_ip_allow:" .. k, "ip_allow" )
						end
					end
					log.day_list( "server_init", ngx.worker.id(), "dc3", "shared_dict_config_ok", k )
				else
					log.day_list( "server_init", ngx.worker.id(), "dc3", "warning_shared_dict_not_config", k )
				end
			end
		end
	end

end


-- 正常退出
FOO.final = function()
	log.debug( "gg final" );

	if  ngx.ctx.gg_final_has_called then
		-- 防止无限循环 只调一次
	else
		ngx.ctx.gg_final_has_called = true;
		for k, v in pairs(FOO.redisMgrDcList) do
			v:final()
		end
	end
end


-- 异常退出
FOO.error_exit = function( txt, trace_info_ )
	FOO.final();
	log.error(txt, trace_info_);
	ns_time.print_cost_all();
	ngx.exit(0);
end



return FOO;
